All Questions
Tagged with pythoncoding-style
76 questions
13votes
3answers
3kviews
Using `any` to indicate a wildcard value
I'm writing a validator class to validate certain request objects against a known format. Rule declarations and the validator will both be written entirely in Python, and I don't need to store the ...
24votes
6answers
9kviews
Best practice for redundant conditions in if-elif-else statements
What is considered better practice? Case 1: if n == 0: doThis() elif n < 0: doThat() elif n > 0: doSomethingElse() Case 2: if n == 0: doThis() elif n < 0: doThat() else: ...
0votes
2answers
620views
Is there some easy way to refactor deeply coupled python code
I recently took on a long ago python project which has some weird code style that I can't pinpoint. e.g. # this is a params and value package? opts={ infile="xxx", outfile="xxx" } ...
2votes
1answer
605views
Does "happy path to the left edge" break Python conventions?
I found the short article Align the happy path to the left edge quite helpful in improving readability of functions. Briefly, reading down the left edge of the function should step you through the ...
-1votes
1answer
3kviews
Best practices for naming python utils / extending core modules?
So a lot of the time my utils end up with a structure that mirrors the core library. I might end up writing a multiline version of str.center, an itertools-y function that returns the first or last ...
14votes
3answers
3kviews
Should I choose repeated code in unit test or test logic? Can I avoid both?
When writing unit tests, I feel that there is a trade-off between code repetition and test logic. Example of my current (likely flawed) approach: To test this function (overly simple function for ...
4votes
1answer
2kviews
Duplicate code for imports Python
In a project I have the same imports in multiple files. For example: import os import logging import json import time import pathlib and pylint will tell me I have duplicate code. I know that there ...
24votes
8answers
7kviews
Is using lambdas to express intent not pythonic?
PEP 8 states the following about using anonymous functions (lambdas) Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier: # Correct: ...
2votes
1answer
469views
How to balance 'efficient' vs 'clean' code? [closed]
I have been coding in python for a little over a year, and I have learned a lot and developed quite a few applications, in the process. I do not program for my profession, I simply program ...
1vote
3answers
175views
Weighting guidelines to choose between a method and a function
Quite often I find difficult to decide between implementing operations as functions or as methods because I am not sure how to weight various well-known guidelines for this problem. I would like to ...
3votes
2answers
1kviews
Python import order, mixing from ... import ... and import ... (and import ... as ...)
This is the mess of imports currently at the top of my file: import argparse from copy import deepcopy from functools import cmp_to_key, partial from itertools import chain import math from ...
7votes
1answer
3kviews
Python: Class vs NamedTuple vs Hybrid vs DataClass
So all four of these approaches to structure data on their surface work more or less the same to keep data well structured. Are there any reasons, be they hidden performance issues/enhancements, ...
3votes
1answer
1kviews
Strictly only importing modules in subdirectories: is this a good rule?
I'm making my 1st official project. It's written in Python, is open-sources, and I'd like people to be able to freely and easily fork and modify the code. The project name is "shelf" and the ...
3votes
5answers
1kviews
When to extract boolean conditions into their own function?
I commonly use a boolean condition/variable when it's something too big or complicated and takes too much space by itself inside ifs - basically to avoid repeatability and thus improve readability. E....
0votes
2answers
257views
Code style to keep track of nested objects and data types?
In untyped languages (Python, Javascript specifically) find myself making a lot of bugs / wasting a lot of time because I forget what's in the objects I'm passing around. For example, I forget things ...